﻿

using System;
using System.Collections.Generic;

namespace LeetCodePractice.Topic
{
    public class T00004_FindMedianSorted : ITestBase
    {
        public void Test()
        {
            var action = (int[] nums1, int[] nums2) =>
            {
                var res = Sum(nums1, nums2);
                Console.WriteLine($"[{string.Join(",", nums1)}] [{string.Join(",", nums2)}]=>  {res}");
            };
            action(new int[] { 1, 3 }, new int[] { 2 });
            action(new int[] { 1, 2 }, new int[] { 3, 4 });
            action(new int[] { 0, 0 }, new int[] { 0, 0 });
            action(new int[] { }, new int[] { 1 });
            action(new int[] { 2 }, new int[] { });
        }
        public double Sum(int[] nums1, int[] nums2)
        {
            int mid1 = 0, mid2 = 0;
            int digit = nums1.Length % 2 + nums2.Length % 2;
            int q = nums1.Length / 2 + nums2.Length / 2 + digit / 2;
            if (q > 0)
            {
                if (digit == 1)
                {
                    mid1 = mid2 = q;
                }
                else
                {
                    mid1 = q - 1;
                    mid2 = q;
                }
            }
            int x1 = 0;
            int x2 = 0;
            int x3 = 0;
            int midres1 = 0;
            int midres2 = 0;

            while (x1 < nums1.Length && x2 < nums2.Length && x3 <= mid2)
            {
                if (nums1[x1] < nums2[x2])
                {
                    if (x3 == mid1)
                    {
                        midres1 = nums1[x1];
                    }
                    if (x3 == mid2)
                    {
                        midres2 = nums1[x1];
                        return midres1 == midres2 ? midres1 : (midres1 + midres2) / 2.0;
                    }
                    x3++;
                    x1++;
                }
                else
                {
                    if (x3 == mid1)
                    {
                        midres1 = nums2[x2];
                    }
                    if (x3 == mid2)
                    {
                        midres2 = nums2[x2];
                        return midres1 == midres2 ? midres1 : (midres1 + midres2) / 2.0;
                    }
                    x3++;
                    x2++;
                }
            }
            while (x1 < nums1.Length && x3 <= mid2)
            {
                if (x3 == mid1)
                {
                    midres1 = nums1[x1];
                }
                if (x3 == mid2)
                {
                    midres2 = nums1[x1];
                    return midres1 == midres2 ? midres1 : (midres1 + midres2) / 2.0;
                }
                x3++;
                x1++;
            }
            while (x2 < nums2.Length && x3 <= mid2)
            {
                if (x3 == mid1)
                {
                    midres1 = nums2[x2];
                }
                if (x3 == mid2)
                {
                    midres2 = nums2[x2];
                    return midres1 == midres2 ? midres1 : (midres1 + midres2) / 2.0;
                }
                x3++;
                x2++;
            }

            return midres1 == midres2 ? midres1 : (midres1 + midres2) / 2.0;
        }
    }
}
